package com.momo.common.req.sysSetting.route;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.momo.mapper.dataobject.RoutesDO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MOMO on 2019/2/27.
 */
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RouteDefinition {
    @NotEmpty
    private String id;
    @NotEmpty
    private String sysName;
    @NotEmpty
    @Valid
    private List<PredicateDefinition> predicates = new ArrayList<>();

    @Valid
    private List<FilterDefinition> filters = new ArrayList<>();

    @NotNull
    private URI uri;

    private int order = 0;


    public static RouteDefinition DOtransformVO(RoutesDO routesDO) {
        List<FilterDefinition> filterDefinitions = JSON.parseObject(routesDO.getSysFilters(), new TypeReference<List<FilterDefinition>>() {
        });
        List<PredicateDefinition> predicateDefinitions = JSON.parseObject(routesDO.getSysPredicates(), new TypeReference<List<PredicateDefinition>>() {
        });
        RouteDefinition routeDefinition = RouteDefinition.builder().id(routesDO.getSysId())
                .order(routesDO.getSysOrder()).uri(URI.create(routesDO.getSysUri()))
                .filters(filterDefinitions).predicates(predicateDefinitions).build();
        return routeDefinition;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<PredicateDefinition> getPredicates() {
        return predicates;
    }

    public void setPredicates(List<PredicateDefinition> predicates) {
        this.predicates = predicates;
    }

    public List<FilterDefinition> getFilters() {
        return filters;
    }

    public void setFilters(List<FilterDefinition> filters) {
        this.filters = filters;
    }

    public URI getUri() {
        return uri;
    }

    public void setUri(URI uri) {
        this.uri = uri;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public String getSysName() {
        return sysName;
    }

    public void setSysName(String sysName) {
        this.sysName = sysName;
    }

    @Override
    public String toString() {
        return "RouteDefinition{" +
                "id='" + id + '\'' +
                ", sysName='" + sysName + '\'' +
                ", predicates=" + predicates +
                ", filters=" + filters +
                ", uri=" + uri +
                ", order=" + order +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RouteDefinition that = (RouteDefinition) o;

        if (order != that.order) return false;
        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (sysName != null ? !sysName.equals(that.sysName) : that.sysName != null) return false;
        if (predicates != null ? !predicates.equals(that.predicates) : that.predicates != null) return false;
        if (filters != null ? !filters.equals(that.filters) : that.filters != null) return false;
        return uri != null ? uri.equals(that.uri) : that.uri == null;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (sysName != null ? sysName.hashCode() : 0);
        result = 31 * result + (predicates != null ? predicates.hashCode() : 0);
        result = 31 * result + (filters != null ? filters.hashCode() : 0);
        result = 31 * result + (uri != null ? uri.hashCode() : 0);
        result = 31 * result + order;
        return result;
    }
}
